﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAControls
{
    public class Panel : XNAControl
    {
        public Texture2D BackgroundTexture { get; set; }
        /// <summary>
        /// Określa część tekstury, która będzie używana. Dla SourceRectangle == null używana jest cała tekstura.
        /// </summary>
        public Rectangle? SourceRectangle { get; set; }
        public Color Color { get; set; }

        public Panel()
            : base()
        {
            Color = Color.White;
            SourceRectangle = null;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (Visible)
            {
                if (BackgroundTexture == null)
                {
                    throw new ArgumentNullException("Texture not set");
                }
                Rectangle? src;
                if (SourceRectangle == null)
                {
                    src = BackgroundTexture.Bounds;
                }
                else
                {
                    src = SourceRectangle;
                }

                spriteBatch.Draw(BackgroundTexture,
                                 new Rectangle(AbsolutePosition.X, AbsolutePosition.Y, Size.X, Size.Y), src, Color);
            }
            base.Draw(spriteBatch);
        }
    }
}
